package com.heima.article.service.impl;

import com.heima.article.mapper.ArticleHomeMapper;
import com.heima.article.service.ArticleHomeService;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ArticleHomeServiceImpl implements ArticleHomeService {
    @Autowired
    private ArticleHomeMapper articleHomeMapper;

    private static final short MAX_PAGE_SIZE = 50;

    @Override
    public ResponseResult loadArticleList(ArticleHomeDto dto, short loadtype) {

        //1.校验参数
        Integer size = dto.getSize();
        if (size == null || size == 0) {
            size = 10;
        }
        size = Math.min(size, MAX_PAGE_SIZE);
        dto.setSize(size);

        //文章频道校验
        if (StringUtils.isEmpty(dto.getTag())) {
            dto.setTag("__all__");
        }

        //时间校验
        if (dto.getMaxBehotTime() == null) dto.setMaxBehotTime(new Date());
        if (dto.getMinBehotTime() == null) dto.setMinBehotTime(new Date());

        if (loadtype != 1 && loadtype != 2) {
            loadtype = 1;
        }

        List<ApArticle> apArticles = articleHomeMapper.loadArticleList(dto, loadtype);
        if (apArticles == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "数据不存在");
        }
        return ResponseResult.okResult(apArticles);
    }

}
